﻿/// <reference path="jquery-1.5.1.min.js" />

////////////////////////////////////////////////////////////////////////////////
//
//  Created by Fernando Cerqueira
//  Copyright (c) 2011.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

var preserveShutDown = "checked";
var currImgBg = 1;
var MaxImgBg = 5;
var WinMenuStyle = "8";
var FindWin8StarMenuKey = false;
var WshShell = new ActiveXObject("WScript.Shell");
var GuidVersion = "EC5A88BD-0271-4DF6-8707-E0F7B5FEBF01|V.1.1.0";
var CheckAuto = "checked";

// --------------------------------------------------------------------
function CurrentVersion() {
    // --------------------------------------------------------------------
    return GuidVersion.split("|", 2)[1];
}

// --------------------------------------------------------------------
function CurrentGuid() {
    // --------------------------------------------------------------------

    return GuidVersion.split("|", 2)[0];
}

// --------------------------------------------------------------------

function IsNewVersion(siteversion) {
    // --------------------------------------------------------------------
    var curver = GuidVersion.split("|", 2)[1];
    if (curver.length != siteversion.length) {
        return false;
    }
    return (siteversion > curver);
}

// --------------------------------------------------------------------
function startup() {
    // --------------------------------------------------------------------

    System.Gadget.onSettingsClosing = SettingsClosing;

    WinMenuStyle = System.Gadget.Settings.read("WinMenuStyle");

    try {
        SKey = WshShell.RegRead("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RPEnabled");
        FindWin8StarMenuKey = true;
    }
    catch (e) {
        FindWin8StarMenuKey = false;
        WinMenuStyle = "7";
    }

    if (WinMenuStyle == "8") {
        $("#OptWin8").attr("checked", "checked");
        $("#OptWin7").attr("checked", "");
    }
    else {
        $("#OptWin8").attr("checked", "");
        $("#OptWin7").attr("checked", "checked");
    }

    preserveShutDown = System.Gadget.Settings.read("preserveShutDown");
    $("#ChkPreserve").attr("checked", preserveShutDown);

    currImgBg = System.Gadget.Settings.read("BackgroundTheme");
    CheckAuto = System.Gadget.Settings.read("CheckAuto");

    $("#ChkUpdate").attr("checked", CheckAuto);

    if (CheckAuto == "") {
        $("#msgupdate").attr("class", "MsgupdateYellow");
        $("#TextUpdate").empty();
    }
    else {
        FindVersion();
    }
}

// --------------------------------------------------------------------
function FindVersion() {
    // --------------------------------------------------------------------
    $("#msgupdate").attr("class", "MsgupdateYellow");
    $("#TextUpdate").text("Wait result...");
    jQuery.support.cors = true;
    $.ajax({
        url: "http://windows8startmenu.codeplex.com/",
        success: function (data) {
            var index = data.indexOf(CurrentGuid() + "|")
            var siteversion = "";
            if (index >= 0) {
                index = index + CurrentGuid().length + 1;
                siteversion = data.slice(index, index + 7);
                if (IsNewVersion(siteversion)) {
                    $("#msgupdate").attr("class", "MsgupdateGreen");
                    $("#TextUpdate").text("Have a new version! " + siteversion);
                }
                else {
                    $("#msgupdate").attr("class", "MsgupdateYellow");
                    $("#TextUpdate").text("No update avaibable. " + siteversion);
                }
            }
            else {
                $("#msgupdate").attr("class", "MsgupdateRed");
                $("#TextUpdate").text("Not find key version at Codeplex.");
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $("#msgupdate").attr("class", "MsgupdateRed");
            $("#TextUpdate").text("Request version Codeplex :" + textStatus);
        }
    });
}

// --------------------------------------------------------------------
function setPrev() {

    // --------------------------------------------------------------------

    currImgBg -= 1;
    if (currImgBg <= 0)
        currImgBg = MaxImgBg;
    setTheme();
}

// --------------------------------------------------------------------
function setNext() {
    // --------------------------------------------------------------------

    currImgBg += 1;
    if (currImgBg > MaxImgBg)
        currImgBg = 1;
    setTheme();
}

// --------------------------------------------------------------------
function setTheme() {
    // --------------------------------------------------------------------

    $("#imgbg").attr("src", "images/bg_" + currImgBg + ".png");
}

// --------------------------------------------------------------------
function SettingsClosing(event) {
    // --------------------------------------------------------------------

    if (event.closeAction == event.Action.commit) {
        System.Gadget.Settings.write("CheckAuto", CheckAuto);
        System.Gadget.Settings.write("preserveShutDown", $("#ChkPreserve").attr("checked"));
        System.Gadget.Settings.write("BackgroundTheme", currImgBg);
        System.Gadget.Settings.write("WinMenuStyle", WinMenuStyle);
    }
    event.cancel = false;
}

// --------------------------------------------------------------------
function setWin8() {
    // --------------------------------------------------------------------

    if (FindWin8StarMenuKey) {
        WinMenuStyle = "8";
        $("#OptWin8").attr("checked", "checked");
        $("#OptWin7").attr("checked", "");
    }
}

// --------------------------------------------------------------------
function setWin7() {
    // --------------------------------------------------------------------

    WinMenuStyle = "7";
    $("#OptWin8").attr("checked", "");
    $("#OptWin7").attr("checked", "checked");
}
